package com.zyw.zhuangdian.dao.impl;

import com.zyw.zhuangdian.dao.BaseDao;
import com.zyw.zhuangdian.dao.FavouriteDao;
import com.zyw.zhuangdian.exceptions.MyDaoException;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Repository
@Transactional
public class FavouriteDaoImpl extends BaseDao implements FavouriteDao {
    @Override
    public void addFavourite(Map in) {
        int i = sqlTemplate.insert("FavouriteDaoImpl.addFavourite",in);
        if (i<1){
            throw new MyDaoException("插入收藏记录失败");
        }
    }

    @Override
    public void updateFavourite(Map in) {
        int i = sqlTemplate.update("FavouriteDaoImpl.updateFavourite",in);
        if (i<1){
            throw new MyDaoException("更新收藏记录失败");
        }
    }

    @Override
    public void deleteFavourite(Map in) {
        int i = sqlTemplate.update("FavouriteDaoImpl.deleteFavourite",in);
        if (i!=1){
            throw new MyDaoException("删除收藏记录失败");
        }
    }

    @Override
    public List<Map> listFavourites(Map in) {
        List<Map> list = sqlTemplate.selectList("FavouriteDaoImpl.listFavourites",in);
        return list;
    }

    @Override
    public List<Map> listFavouriteProductions(Map in) {
        List<Map> list = sqlTemplate.selectList("FavouriteDaoImpl.listFavouriteProductions",in);
        return list;
    }

    @Override
    public List<Map> listFavouriteArticles(Map in) {
        List<Map> list = sqlTemplate.selectList("FavouriteDaoImpl.listFavouriteArticles",in);
        return list;
    }

    @Override
    public int listFavouritesCount(Map in) {
        List<Map> list = sqlTemplate.selectList("FavouriteDaoImpl.listFavouritesCount",in);
        if (list.size() < 1){
            return 0;
        }
        return Integer.parseInt(list.get(0).get("count").toString());
    }
}
